class PageInfo(object):
    def __init__(self, current_page, all_count, base_url, per_page, showpage):
        '''
        :param current_page:
        :param all_count: 数据总个数
        :param per_page: 每页显示个数
        '''
        try:
            self.current_page = int(current_page)
        except Exception as e:
            self.current_page = 1
        self.per_page = per_page

        a, b = divmod(all_count, per_page)
        if b:
            a = a + 1
        self.all_pager = a  # all_pager为总的页码数
        # if a / per_page <= 1:
        #     self.showpage = all_count
        self.showpage = showpage
        self.base_url = base_url

    def start(self):
        return (self.current_page - 1) * self.per_page

    def end(self):
        return self.current_page * self.per_page

    def pager(self):
        half = int((self.showpage - 1) // 2)
        # if self.current_page <= half:
        #     begin = 1
        #     stop = self.showpage + 1
        # elif self.current_page >= self.all_pager - half:
        #     begin = self.all_pager - self.showpage
        #     stop = self.all_pager + 1
        # else:
        #     begin = self.current_page - half
        #     stop = self.current_page + half + 1

        # 如果总页数<showpage
        if self.all_pager < self.per_page:
            begin = 1
            stop = self.all_pager + 1
        else:
            if self.current_page <= half:
                begin = 1
                stop = self.showpage + 1
            else:
                if self.current_page + half > self.all_pager:
                    begin = self.all_pager - self.showpage + 1
                    stop = self.all_pager + 1
                else:
                    begin = self.current_page - half
                    stop = self.current_page + half + 1

        page_list = []
        if self.current_page <= 1:
            prev = '<li><a href = "#">上一页</a></li>'
        else:
            prev = '<li><a href = "%s?page=%s">上一页</a></li>' % (self.base_url, self.current_page - 1,)
        page_list.append(prev)
        for i in range(begin, stop):
            if i == self.current_page:
                tmp = '<li class="active"><a href = "%s?page=%s"> %s </a></li>' % (self.base_url, i, i)
            else:
                tmp = '<li><a href = "%s?page=%s"> %s </a></li>' % (self.base_url, i, i)
            page_list.append(tmp)
        if self.current_page >= self.all_pager:
            nex = '<li><a href = "#">下一页</a></li>'
        else:
            nex = '<li><a href = "%s?page=%s">下一页</a></li>' % (self.base_url, self.current_page + 1,)
        page_list.append(nex)
        return ''.join(page_list)
